package com.data.modules.smzh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.smzh.entity.Deviceuser;
import com.data.modules.smzh.entity.Personfacesync;
import com.data.modules.smzh.service.IDeviceuserService;
import com.data.modules.smzh.service.IPersonfacesyncService;
import com.data.modules.smzh.vo.SmUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: deviceuser
 * @Author: data-boot
 * @Date:   2022-12-01
 * @Version: V1.0
 */
@Api(tags="deviceuser")
@RestController
@RequestMapping("/smzh/deviceuser")
@Slf4j
public class DeviceuserController extends BootController<Deviceuser, IDeviceuserService> {
	@Autowired
	private IDeviceuserService deviceuserService;

	@Autowired
	private IPersonfacesyncService personfacesyncService;


	/**
	 * 分页列表查询
	 *
	 * @param deviceuser
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "deviceuser-分页列表查询")
	@ApiOperation(value="deviceuser-分页列表查询", notes="deviceuser-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Deviceuser deviceuser,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Deviceuser> queryWrapper = QueryGenerator.initQueryWrapper(deviceuser, req.getParameterMap());
		Page<Deviceuser> page = new Page<Deviceuser>(pageNo, pageSize);
		IPage<Deviceuser> pageList = deviceuserService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@AutoLog(value = "deviceuser-分页列表查询")
	@ApiOperation(value="deviceuser-分页列表查询", notes="deviceuser-分页列表查询")
	@GetMapping(value = "/pageList")
	public Result<?> queryDeviceUserPageList(SmUserInfoVO deviceuser,
									   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									   HttpServletRequest req) {
		QueryWrapper<SmUserInfoVO> queryWrapper = QueryGenerator.initQueryWrapper(deviceuser, req.getParameterMap());
		Page<SmUserInfoVO> page = new Page<SmUserInfoVO>(pageNo, pageSize);

		IPage<SmUserInfoVO> pageList = deviceuserService.queryDeviceUserPageList(page, deviceuser);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param deviceuser
	 * @return
	 */
	@AutoLog(value = "deviceuser-添加")
	@ApiOperation(value="deviceuser-添加", notes="deviceuser-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Deviceuser deviceuser) {
		deviceuserService.save(deviceuser);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param deviceuser
	 * @return
	 */
	@AutoLog(value = "deviceuser-编辑")
	@ApiOperation(value="deviceuser-编辑", notes="deviceuser-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Deviceuser deviceuser) {
		deviceuserService.updateById(deviceuser);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "deviceuser-通过id删除")
	@ApiOperation(value="deviceuser-通过id删除", notes="deviceuser-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		deviceuserService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "deviceuser-批量删除")
	@ApiOperation(value="deviceuser-批量删除", notes="deviceuser-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {

		List<String> list = Arrays.asList(ids.split(","));

		list.stream().forEach(x->{

			Deviceuser deviceuser = deviceuserService.getById(x);

			Personfacesync personfacesync = new Personfacesync();
			personfacesync.setCmd(2);//删除
			personfacesync.setSync(0);
			personfacesync.setErrorValue("0");
			personfacesync.setUserId(deviceuser.getUserId().toString());
			personfacesync.setSerialNo(deviceuser.getSerialNo());

			personfacesyncService.save(personfacesync);

		});


		this.deviceuserService.removeByIds(Arrays.asList(ids.split(",")));

		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "deviceuser-通过id查询")
	@ApiOperation(value="deviceuser-通过id查询", notes="deviceuser-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Deviceuser deviceuser = deviceuserService.getById(id);
		if(deviceuser==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(deviceuser);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param deviceuser
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Deviceuser deviceuser) {
        return super.exportXls(request, deviceuser, Deviceuser.class, "deviceuser");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Deviceuser.class);
    }

}
